package com.example.weight.greenSeasons.waterDeal;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.weight.R;
import com.example.weight.config.Config;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class EquipmentMonthlyMaintainNavTable_old extends AppCompatActivity {

    private String name, role, factory;

    private Button tiaojiechi, jiaobanji, Ochifengji1, Ochifengji2, mochifengji1,
            mochifengji2, mochifengji3, huiliubeng, Ochi, mochi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_equipment_monthly_maintain_nav_table);

        Intent previousIntent = getIntent();
        name = previousIntent.getStringExtra("name");
        role = previousIntent.getStringExtra("role");
        factory = previousIntent.getStringExtra("factory");

        TitleBar titleBar = findViewById(R.id.titleBar);
        titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                finish();
            }

            @Override
            public void onTitleClick(View v) {}

            @Override
            public void onRightClick(View v) {}
        });


        tiaojiechi = findViewById(R.id.tiaojiechi);
        jiaobanji = findViewById(R.id.jiaobanji);
        Ochifengji1 = findViewById(R.id.Ochifengji1);
        Ochifengji2 = findViewById(R.id.Ochifengji2);
        mochifengji1 = findViewById(R.id.mochifengji1);
        mochifengji2 = findViewById(R.id.mochifengji2);
        mochifengji3 = findViewById(R.id.mochifengji3);
        huiliubeng = findViewById(R.id.huiliubeng);
        Ochi = findViewById(R.id.Ochi);
        mochi = findViewById(R.id.mochi);

        RequestQueue queue = Volley.newRequestQueue(this);
        String url = Config.url + "/shlj/sjqWaterProductionEquipmentMaintenanceMaintenance/is_month_complete";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, response -> {
            try {
                JSONObject jsonObject = new JSONObject(response);
                if (!jsonObject.isNull("data")) {
                    JSONArray data = jsonObject.getJSONArray("data");
                    int checkedColor = Color.parseColor("#66CC66");
                    int i = 0;
                    if (data.getString(i++).equals("true"))
                        tiaojiechi.setBackgroundColor(checkedColor);
                    if (data.getString(i++).equals("true"))
                        jiaobanji.setBackgroundColor(checkedColor);
                    if (data.getString(i++).equals("true"))
                        Ochifengji1.setBackgroundColor(checkedColor);
                    if (data.getString(i++).equals("true"))
                        Ochifengji2.setBackgroundColor(checkedColor);
                    if (data.getString(i++).equals("true"))
                        mochifengji1.setBackgroundColor(checkedColor);
                    if (data.getString(i++).equals("true"))
                        mochifengji2.setBackgroundColor(checkedColor);
                    if (data.getString(i++).equals("true"))
                        mochifengji3.setBackgroundColor(checkedColor);
                    if (data.getString(i).equals("true"))
                        huiliubeng.setBackgroundColor(checkedColor);
                    if (data.getString(i++).equals("true"))
                        Ochi.setBackgroundColor(checkedColor);
                    if (data.getString(i).equals("true"))
                        mochi.setBackgroundColor(checkedColor);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }, error -> Toast.makeText(EquipmentMonthlyMaintainNavTable_old.this,
                "获取信息失败 " + error.getMessage(), Toast.LENGTH_SHORT).show()) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> map = new HashMap<>();
                map.put("equipmentsName", "小调节池潜水泵_搅拌机_1#O池风机_2#O池风机_" +
                        "1#膜池风机_2#膜池风机_3#膜池风机_污泥回流泵_O池电控系统_膜池电控系统");
                map.put("menstanceNum", "3_3_4_4_4_4_4_3_4_4");
                map.put("role", role.equals("操作人")?"operator":"examiner");
                map.put("factory", factory);
                return map;
            }
        };
        stringRequest.setRetryPolicy(new DefaultRetryPolicy(10 * 1000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        queue.add(stringRequest);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            boolean needRefresh = data.getBooleanExtra("needRefresh", true);
            if (needRefresh)
                refresh();
        }
    }

    private void go(String fName) {
        Intent intent;
        if (role.equals("操作人"))
            intent = new Intent(this, EquipmentMonthlyMaintainTable_old.class);
        else
            intent = new Intent(this, EquipmentMonthlyInspectorTable_old.class);
//        Intent intent =new Intent(EquipmentMonthlyMaintainNavTable.this,
//                EquipmentMonthlyMaintainTable.class);
        intent.putExtra("name", name);
        intent.putExtra("role", role);
        intent.putExtra("fName", fName);
        intent.putExtra("factory", factory);
        startActivityForResult(intent, 1);
    }

    public void gotiaojiechi(View view) {
        go("小调节池潜水泵");
    }
    public void gojiaobanji(View view) {
        go("搅拌机");
    }
    public void goOchifengji1(View view) {
        go("1#O池风机");
    }
    public void goOchifengji2(View view) {
        go("2#O池风机");
    }
    public void gomochifengji1(View view) {
        go("1#膜池风机");
    }
    public void gomochifengji2(View view) {
        go("2#膜池风机");
    }
    public void gomochifengji3(View view) {
        go("3#膜池风机");
    }
    public void gohuiliubeng(View view) {
        go("污泥回流泵");
    }
    public void goOchi(View view) {
        go("O池电控系统");
    }
    public void gomochi(View view) {
        go("膜池电控系统");
    }

    // 刷新activity
    public void refresh() {
        finish();
        Intent intent = new Intent(EquipmentMonthlyMaintainNavTable_old.this,
                EquipmentMonthlyMaintainNavTable_old.class);
        intent.putExtra("name", name);
        intent.putExtra("role", role);
        intent.putExtra("factory", factory);
        startActivity(intent);
    }
}